package com.example.mansystem.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mansystem.dto.ChangePermissionDto;
import com.example.mansystem.entity.RoleDO;

import com.example.mansystem.entity.UserDO;
import com.example.mansystem.exception.CodeAndMsg;
import com.example.mansystem.exception.UserDefinedException;
import com.example.mansystem.mapper.UserMapper;
import com.example.mansystem.vo.RouteApiList;
import org.assertj.core.internal.bytebuddy.implementation.bytecode.Throw;
import org.springframework.stereotype.Service;
import com.example.mansystem.mapper.RoleMapper;
import javax.annotation.Resource;
import java.util.List;

@Service
public class RoleService extends ServiceImpl<RoleMapper, RoleDO> {
    @Resource
    RoleMapper RoleMapper;

    @Resource
    UserMapper userMapper;

    public List<RoleDO> getRoleList()
    {
        return this.list();
    }
    public void addRole(String rolename)
    {
        RoleMapper.addRole(rolename);
    }
    public List<String> getRouteByRoleId(int id)
    {
        return RoleMapper.getRouteByRoleId(id);
    }

    public List<String> getApiByRoleId(int id)
    {
        return RoleMapper.getApiByRoleId(id);
    }


    public String getRoleNameByUsername(String username)
    {
        QueryWrapper<UserDO> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(UserDO::getUsername, username);
        List<UserDO> userDOList = userMapper.selectList(wrapper);
        if(userDOList.size()!=1)
            throw new UserDefinedException(CodeAndMsg.USER_EXISTED);
        else
        {
            int roleId=userDOList.get(0).getRoleid();
            QueryWrapper<RoleDO> roleDOQueryWrapper = new QueryWrapper<>();
            roleDOQueryWrapper.lambda().eq(RoleDO::getRoleId, roleId);
            List<RoleDO> roleDOList = RoleMapper.selectList(roleDOQueryWrapper);
            return roleDOList.get(0).getRoleName();
        }
    }


    public List<RoleDO> getAllRole()
    {
        return RoleMapper.selectList(null);
    }
    public RouteApiList getRouteApiList()
    {
        return RouteApiList.builder().route(RoleMapper.getAllRoute()).api(RoleMapper.getAllApi()).build();
    }

    public void ChangePermission(ChangePermissionDto dto)
    {
        if(dto.getRoleId()==1)
            throw new UserDefinedException(CodeAndMsg.AdminRoleCantChange);
        for(int i=0;i<dto.getRouteInsert().size();i++)
        {
            RoleMapper.insertRoutePoession(dto.getRoleId(),dto.getRouteInsert().get(i));
        }

        for(int i=0;i<dto.getRouteDelete().size();i++)
        {
            RoleMapper.deleteRoutePoession(dto.getRoleId(),dto.getRouteDelete().get(i));
        }

        for(int i=0;i<dto.getApiInsert().size();i++)
        {
            RoleMapper.insertApiPoession(dto.getRoleId(),dto.getApiInsert().get(i));
        }

        for(int i=0;i<dto.getApiDelete().size();i++)
        {
            RoleMapper.deleteApiPoession(dto.getRoleId(),dto.getApiDelete().get(i));
        }
    }

    public void deleteRole(int roleId)
    {
        if(roleId==1)
            throw new UserDefinedException(CodeAndMsg.AdminCantChange);
        RoleMapper.deleteRole(roleId);
    }
}
